﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.SqlClient;

/// <summary>
///AdminRole 的摘要说明
/// </summary>
public partial class AdminRole
{
    public static AdminRole Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminRole.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<AdminRole> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminRole.ToList();
        }
    }


    public static List<AdminRole> GetAllByConditon(string RoleName)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from ar in context.AdminRole
                         select ar;
            if (!string.IsNullOrEmpty(RoleName))
                query = query.Where(row => SqlMethods.Like(row.RoleName,"%" + RoleName+"%"));
            return query.ToList();
        }
    }


    //执行执行sql语句
    //public static List<AdminRole> GetAllByConditon1(string RoleName)
    //{
    //    using (DataClassesDataContext context = new DataClassesDataContext())
    //    {
    //        //return context.ExecuteCommand("",RoleName);直接执行update方法   

    //        object[] pams = { 3, "%管理员", "%管理员", "%管理员", "%管理员" };

    //        UpdateAll(pams);
    //        var query = context.ExecuteQuery<AdminRole>(@"Select * from dbo.AdminRole where RoleName  LIKE {1} and ID={0} ", pams);
    //        return query.ToList();
    //    }
    //}


    //进行批量更新操作
    //public static int UpdateAll(object[]pams)
    //{
    //    using (DataClassesDataContext context = new DataClassesDataContext())
    //    {

    //        return context.ExecuteCommand(@"Update  dbo.AdminRole set RoleName='叶修涛管理员' where RoleName  LIKE {1} and ID={0} ", pams);
    //    }
    //}



    //例子参考
    //public static List<AdminRole> GetAllByConditon(string RoleName)
    //{
    //    using (DataClassesDataContext context = new DataClassesDataContext())
    //    {
    //        var query = (from ar in context.AdminRole
    //                     join u in context.AdminUser
    //                     on ar.ID equals u.ID
    //                     into grp
    //                     from u1 in grp.DefaultIfEmpty()
    //                     //不建议用这个语法 但是SqlMethods 有很多很好的方法  可用 比如like
     //                     //where SqlMethods.DateDiffDay(u1.LoginTime,DongDong.Common.Misc.ConvertToDateTime("2012-11-12"))>0
    //                     where u1.LoginTime >= DongDong.Common.Misc.ConvertToDateTime("2012-11-12")
    //                     select ar);
    //        if (!string.IsNullOrEmpty(RoleName))
    //            query = query.Where(row => row.RoleName.Contains(RoleName));

    //        return query.ToList();
    //    }
    //}



    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
           
            context.AdminRole.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<AdminRole>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.AdminRole.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.AdminRole.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

}